package light.head.controller.mobile;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.OrganTypeHandleDAO;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.SystemInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.C_MOBILE_MAIN)
public class MobileMainController extends SupporterController {

	private OrganDAO organDAO;
	private OrganTypeHandleDAO othDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init (HttpServletRequest request, HttpServletResponse response) {
		
		String cu_email = cu_email(request);
//		Integer cu_organCode = cu_organCode(request);
		if ((null == cu_roleCode(request) || null == cu_email) && !Sys.ADMIN.equals(cu_email) && (null == cu_isManager(request) || !cu_isManager(request))) {
			return new ModelAndView(Url.INDEX);
		}
		
		/********** 获取菜单 **********/
		if (!Sys.ADMIN.equals(cu_email)) {
			Map<String, Boolean> handleMap = getCurrentUserHandleMap(request);
			request.setAttribute(Parm.HANDLE_MAP, handleMap);
			
			// 如果是根机构管理员才能看到所有权限，如果是子机构管理员，没有角色也是不能登陆的
			Integer ss = organDAO.getRoot(cu_organCode(request)).getOtc();			// 获取根机构的所有权限
			Map<String, Boolean> orgMap = othDAO.getHandleMapByOrganTypeCode(ss);
			request.setAttribute(Parm.ORG_HANDLE_MAP, orgMap);
		}
//		List<Module> menu = Cache.moduleList;
		List<SystemInfo> systemInfoList = Cache.systemInfoList;
//		Integer ss = (Integer) request.getSession().getAttribute(Parm.CU_ORGAN_TYPE_CODE);
	
		request.setAttribute(Parm.LIST, systemInfoList);
		/****************************/
		
		
		
		return new ModelAndView(Url.MOBILE_MAIN);
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

	@Autowired
	public void setOthDAO(OrganTypeHandleDAO othDAO) {
		this.othDAO = othDAO;
	}
	
	
	
}
